import { _decorator, Component, EPhysics2DDrawFlags, Input, Node, PhysicsSystem2D, RigidBody2D, tween, Tween, Vec2, Vec3 } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { Ball } from './Ball';
import { Board } from './Board';
import { GlobaInterface } from '../../Data/GlobaInterface';
import { GAME_POP, GlobalData } from '../../Data/GlobalData';
import { CoinLoad } from './CoinLoad';
import { PopAd } from '../../Pop/PopAd';
import { ElementLoad } from './ElementLoad';
const { ccclass, property } = _decorator;

@ccclass('GameScene')
/**
 * 游戏场景
 */
export class GameScene extends BaseComponent {

    //操作层
    @property(Node)
    private TouchLoad: Node = null;

    //球
    @property(Node)
    private Ball: Node = null;

    //球阴影
    @property(Node)
    private BallShadow: Node = null;

    //跳跃速度
    private JumpSpeed: number = 50;

    //是否是左侧
    public IsLeft: boolean = true;

    //左侧篮板
    @property(Node)
    private LeftBoard: Node = null;

    //右侧篮板
    @property(Node)
    private RightBoard: Node = null;

    //金币层
    @property(Node)
    private CoinLoad: Node = null;

    //元素曾
    @property(Node)
    private ElementLoad: Node = null;


    protected Init(): void {
        // PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
        //     EPhysics2DDrawFlags.Pair |
        //     EPhysics2DDrawFlags.CenterOfMass |
        //     EPhysics2DDrawFlags.Joint |
        //     EPhysics2DDrawFlags.Shape;
        this.TouchLoad.on(Input.EventType.TOUCH_START, this.TouchCb, this);
    }

    protected OnShow(): void {
        this.Ball.getComponent(Ball).UpdateBallSytle(app.gameData.GameData.basket);
        this.GameStart();
    }

    /**
     * 游戏开始
     */
    public GameStart() {
        GlobalData.IsAd = true;
        GlobalData.GameOver = false;
        this.IsLeft = true;
        this.RightBoard.setPosition(new Vec3(1000, 0));
        this.LeftBoard.setPosition(new Vec3(-1000, 0));
        this.LeftBoardShow();
        this.Ball.setPosition(new Vec3(0, 0));
        this.Ball.getComponent(RigidBody2D).linearVelocity = new Vec2(0, 0);
    }


    /**
     * 左侧篮板出现
     */
    private LeftBoardShow() {
        Tween.stopAllByTarget(this.LeftBoard);
        let pos_y: number = -200 + Math.random() * 600;
        this.LeftBoard.setPosition(new Vec3(-1000, pos_y));
        tween(this.LeftBoard).to(0.3, { position: new Vec3(-350, pos_y) }).call(() => {
            this.LeftBoard.getComponent(Board).ShowPhysics(true);
        }).start();
        this.CoinLoad.getComponent(CoinLoad).AddCoinCount();
    }

    /**
     * 右侧篮板出现
     */
    private RightBoardShow() {
        Tween.stopAllByTarget(this.RightBoard);
        let pos_y: number = -200 + Math.random() * 600;
        this.RightBoard.setPosition(new Vec3(1000, pos_y));
        tween(this.RightBoard).to(0.3, { position: new Vec3(350, pos_y) }).call(() => {
            this.RightBoard.getComponent(Board).ShowPhysics(true);
        }).start();
        this.CoinLoad.getComponent(CoinLoad).AddCoinCount();
    }

    /**
     * 游戏得分
     */
    public GameScore() {
        this.scheduleOnce(() => {
            this.LeftBoard.setPosition(new Vec3(-1000, 0));
            this.LeftBoard.getComponent(Board).ShowPhysics(false);
            this.RightBoard.setPosition(new Vec3(1000.0))
            this.RightBoard.getComponent(Board).ShowPhysics(false);
            if (this.IsLeft) {
                this.LeftBoardShow();
            } else {
                this.RightBoardShow();
            }
        })
    }

    /**
     * 用户点击
     */
    private TouchCb() {
        this.Ball.getComponent(Ball).IamJump(this.IsLeft);
    }

    protected update(dt: number): void {
        if (this.Ball.getPosition().x <= -400) {
            this.Ball.setPosition(375, this.Ball.getPosition().y);
        } else if (this.Ball.getPosition().x >= 400) {
            this.Ball.setPosition(-375, this.Ball.getPosition().y);
        }

        this.BallShadow.setPosition(this.Ball.getPosition().x, 48);
    }

    /**
     * 游戏复活
     */
    public IamSave() {
        app.popManager.HidePop(GAME_POP.PopAd);
        GlobalData.GameOver = false;
        GlobalData.IsAd = false;
        this.ElementLoad.getComponent(ElementLoad).UpdateControlTime();
    }

    /**
     * 游戏结束
     */
    public GameOver() {
        if (GlobalData.GameOver) return;

        GlobalData.GameOver = true;
        if (GlobalData.IsAd) {
            app.popManager.ShowPop(GAME_POP.PopAd);
            app.popManager.CurrentPop.getComponent(PopAd).SaveCb = this.IamSave.bind(this);
            app.popManager.CurrentPop.getComponent(PopAd).CloseCb = () => {
                app.popManager.HidePop(GAME_POP.PopAd);
                app.popManager.ShowPop(GAME_POP.PopEnd);
            }
        } else {
            app.popManager.ShowPop(GAME_POP.PopEnd);
        }


    }

}


